package cn.wolfcode.redis;

import lombok.Getter;

import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;

/**
 * Created by wolfcode
 */
@Getter
public enum SeckillRedisKey {
    SECKILL_PRODUCT_LIST("today:seckill:products"),
    SECKILL_ORDER_HASH("seckillOrderHash"),
    SECKILL_STOCK_COUNT_HASH("seckillStockCount"),
    SECKILL_REAL_COUNT_HASH("seckillRealCount"),
    SECKILL_STOCK_COUNT_LOCK_KEY_STRING("seckill:product:stockcount");


    SeckillRedisKey(String prefix, TimeUnit unit, int expireTime) {
        this.prefix = prefix;
        this.unit = unit;
        this.expireTime = expireTime;
    }

    SeckillRedisKey(String prefix) {
        this.prefix = prefix;
    }

    public String join(Object ...suffix) {
        if (suffix.length == 0) {
            return this.prefix;
        }
        StringJoiner readKey = new StringJoiner(":");
        readKey.add(this.prefix);
        for (Object s : suffix) {
            if (s != null) {
                readKey.add(s.toString());
            }
        }
        return readKey.toString();
    }

    private String prefix;
    private TimeUnit unit;
    private int expireTime;
}
